from flask import Flask, render_template, request, redirect, url_for, flash
import os

app = Flask(__name__)
app.secret_key = "your_secret_key"  # 用于flash消息

# 确保上传文件夹存在
UPLOAD_FOLDER = 'excel-import'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        flash('请选择文件后再上传', 'error')
        return redirect(url_for('index'))
    
    file = request.files['file']
    
    if file.filename == '':
        flash('请选择文件后再上传', 'error')
        return redirect(url_for('index'))
    
    if file:
        # 保存文件
        filename = file.filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        flash(f'文件 {filename} 上传成功', 'success')
        return redirect(url_for('index'))
    
    return redirect(url_for('index'))

if __name__ == '__main__':
    # 可以根据需要修改端口号，默认是5000
    port = 8080  # 修改为您想要的端口号
    app.run(host='0.0.0.0', port=port, debug=True)
